package com.hzya.frame.system.module.controller;

import com.alibaba.fastjson.JSON;
import com.hzya.frame.system.dictionaryshop.entity.DictionaryShopEntity;
import com.hzya.frame.system.dictionaryshop.service.IDictionaryShopService;
import com.hzya.frame.system.module.entity.ModuleEntity;
import com.hzya.frame.system.module.service.IModuleService;
import com.hzya.frame.utils.core.action.DefaultController;
import com.hzya.frame.utils.core.entity.JsonResultEntity;
import com.hzya.frame.utils.core.page.PageInfo;
import com.hzya.frame.utils.core.tree.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @description: 模块  controller
 * @tableName: system_module
 * @entityName: ModuleEntity
 * 包含：
 * 1、分页查询(queryPaged)
 * 2、index页面(goIndexPage)
 * 3、新增页面(goAddPage)
 * 4、新增信息保存(save)
 * 5、修改页面(goEditPage)
 * 6、修改信息保存(update)
 * 7、信息查看(goShowPage)
 * 8、删除信息(delete)
 * @author: gjh
 * @history: 1.0
 */

@Controller("system_module")
@RequestMapping("/system/module/ModuleController")
public class ModuleController extends DefaultController {
    @Autowired
    protected IModuleService moduleService;
    /**
     * 数据字典service
     */
    @Autowired
    private IDictionaryShopService dictionAryService;

    /**
     * 模块分页信息
     **/
    @RequestMapping(value = "queryPaged")
    @ResponseBody
    public PageInfo<ModuleEntity> queryPaged(ModuleEntity entity) throws Exception {
        PageInfo<ModuleEntity> page = null;
        try {
            entity.setSort("order_num");
            entity.setOrder("ASC");
            page = moduleService.queryPaged(entity);
            fixDictionaryContent(page);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

    /**
     * 跳转到模块的index页面
     **/
    @RequestMapping(value = "goIndexPage", method = RequestMethod.GET)
    public ModelAndView goIndexPage() {
        ModelAndView view = new ModelAndView();
        /**这里根据实际的需要填写需要传到前台的对象**/
        //view.addObject(attributeName, attributeValue);
        /**这里是返回对应的页面路径**/
        view.setViewName("/system/module/module/list");
        return view;
    }

    /**
     * 跳转到模块信息新增页面
     **/
    @RequestMapping(value = "goAddPage")
    public ModelAndView goAddPage(Long id) {
        ModelAndView view = new ModelAndView();
        /**这里根据实际的需要填写需要传到前台的对象**/
        ModuleEntity entity = moduleService.get(id);
        view.addObject("entity", entity);
        /** 模块类型*/
        List<DictionaryShopEntity> moduleTypes = dictionAryService.queryDictionaries("system_module", "type");
        view.addObject("moduleTypeList", moduleTypes);

        /** 模块操作级别*/
        List<DictionaryShopEntity> moduleLevelList = dictionAryService.queryDictionaries("system_module", "level");
        view.addObject("moduleLevelList", moduleLevelList);
        /**这里是返回对应的页面路径**/
        view.setViewName("/system/module/module/add");
        return view;
    }

    /**
     * 模块新增信息保存
     **/
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity save(ModuleEntity entity) {
        try {
            moduleService.save(entity);
            return getSuccessMessageEntity("保存成功", "save",entity);
        } catch (Exception e) {
            logger.error("save", e);
            e.printStackTrace();
            return getFailureMessageEntity("保存失败！" + e.getMessage(), "save");
        }
    }

    /**
     * 跳转到模块信息修改页面
     **/
    @RequestMapping(value = "edit/{id}")
    public ModelAndView goEditPage(@PathVariable("id") Long id) {
        ModelAndView view = new ModelAndView();
        /**这里根据实际的需要填写需要传到前台的对象**/
        ModuleEntity entity = moduleService.get(id);
        view.addObject("entity", entity);
        /** 模块类型*/
        List<DictionaryShopEntity> moduleTypes = dictionAryService.queryDictionaries("system_module", "type");
        view.addObject("moduleTypeList", moduleTypes);
        /** 模块操作级别*/
        List<DictionaryShopEntity> moduleLevelList = dictionAryService.queryDictionaries("system_module", "level");
        view.addObject("moduleLevelList", moduleLevelList);
        /**这里是返回对应的页面路径**/
        view.setViewName("/system/module/module/edit");
        return view;
    }

    /**
     * 模块修改信息保存
     **/
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity update(ModuleEntity entity) {
        try {
            moduleService.update(entity);
            return getSuccessMessageEntity("更新成功！", "update",entity);
        } catch (Exception e) {
            logger.error("update", e);
            e.printStackTrace();
            return getFailureMessageEntity("更新失败！" + e.getMessage(), "update");
        }
    }

    /**
     * 跳转到模块信息查看
     **/
    @RequestMapping(value = "show/{id}")
    public ModelAndView goShowPage(@PathVariable("id") Long id) {
        ModelAndView view = new ModelAndView();
        /**这里根据实际的需要填写需要传到前台的对象**/
        ModuleEntity entity = moduleService.get(id);
        view.addObject("entity", entity);
        /** 模块类型*/
        List<DictionaryShopEntity> moduleTypes = dictionAryService.queryDictionaries("system_module", "type");
        view.addObject("moduleTypeList", moduleTypes);
        /** 模块操作级别*/
        List<DictionaryShopEntity> moduleLevelList = dictionAryService.queryDictionaries("system_module", "level");
        view.addObject("moduleLevelList", moduleLevelList);
        /**这里是返回对应的页面路径**/
        view.setViewName("/system/module/module/show");
        return view;
    }

    /**
     * 删除模块信息
     **/
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity delete(Long id) {
        try {
            moduleService.logicRemove(id);
            return getSuccessMessageEntity("删除成功！", "delete");
        } catch (Exception e) {
            logger.error("delete", e);
            e.printStackTrace();
            return getFailureMessageEntity("删除失败！" + e.getMessage(), "delete");
        }
    }

    /**
     * 创建缺省按钮
     * @param entity
     * @return
     */
    @RequestMapping(value = "createDefaultButton", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity createDefaultButton(@RequestBody ModuleEntity entity) {
        try {
            moduleService.createDefaultButton(entity);
            return getSuccessMessageEntity("创建缺省按钮成功！", "add");
        } catch (Exception e) {
            logger.error("add", e);
            e.printStackTrace();
            return getFailureMessageEntity("创建缺省按钮失败！" + e.getMessage(), "delete");
        }
    }

    /**
     * 创建根节点
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/createModuleTree", method = {RequestMethod.GET})
    @ResponseBody
    public List<TreeNode> createModuleTree(@ModelAttribute ModuleEntity entity) {

        List<TreeNode> nodeList = null;
        try {
            nodeList = moduleService.createModuleTree(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nodeList;
    }

    /**
     * 创建子节点
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/createSubModuleTree", method = {RequestMethod.GET})
    @ResponseBody
    public List<TreeNode> createSubModuleTree(@ModelAttribute ModuleEntity entity) {
        List<TreeNode> nodeList = null;
        try {
            nodeList = moduleService.createSubModuleTree(entity);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return nodeList;
    }
}

